package com.yuqiang.manage.service.impl;

import com.yuqiang.common.model.ResultModel;
import com.yuqiang.common.util.DateGetUtil;
import com.yuqiang.manage.dao.ManageDao;
import com.yuqiang.manage.model.bo.*;
import com.yuqiang.manage.model.po.*;
import com.yuqiang.manage.model.vo.*;
import com.yuqiang.manage.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/5 0005.
 */
@Service("manageService")
public class ManageServiceImpl implements ManageService {

    @Autowired
    private ManageDao manageDao;
    //查询获取所有器材列表-分页
    @Override
    public List<EinfoVo> getEquipmentInfoList(EinfoBo bo) {
        return manageDao.getEquipmentInfoList(bo);
    }
    //获取器材表数据条数
    @Override
    public int getEquipmentInfoTotal(EinfoBo bo) {
        return manageDao.getEquipmentInfoTotal(bo);
    }
    //修改器材信息
    @Override
    public void updateEquipmentInfo(EinfoPo po) {
        manageDao.updateEquipmentInfo(po);
    }
    //录入器材信息
    @Override
    public void insertEquipmentInfo(EinfoPo po) {
        manageDao.insertEquipmentInfo(po);
    }


    //器材预定详细信息查询-分页
    @Override
    public List<ReserveRecordVo> getReserveRecordList(ReserveRecordBo bo) {
        return manageDao.getReserveRecordList(bo);
    }
    //器材预定详细信息条数
    @Override
    public int getReserveRecordTotal(ReserveRecordBo bo) {
        return manageDao.getReserveRecordTotal(bo);
    }
    //预定器材
    @Override
    public void reserveEquipment(ReserveRecordPo po) {
        manageDao.reserveEquipment(po);
    }
    //结束预定
    @Override
    public void stopReserveEquipment(ReserveRecordPo po) {
        manageDao.stopReserveEquipment(po);
    }



    //器材使用详细信息查询-分页
    @Override
    public List<UsedRecordVo> getUsedRecordList(UsedRecordBo bo) {
        return manageDao.getUsedRecordList(bo);
    }
    //器材使用详细信息条数
    @Override
    public int getUsedRecordTotal(UsedRecordBo bo) {
        return manageDao.getUsedRecordTotal(bo);
    }
    //使用器材
    @Override
    public void useEquipment(UsedRecordPo po) {
        manageDao.useEquipment(po);
    }
    //结束使用
    @Override
    public void stopUseEquipment(UsedRecordPo po) {
        manageDao.stopUseEquipment(po);
    }


    //查询获取所有锻炼计划列表-分页
    @Override
    public List<ExercisePlanVo> getExercisePlanList(ExercisePlanBo bo) {
        return manageDao.getExercisePlanList(bo);
    }
    //获取所有锻炼计划总条数
    @Override
    public int getExercisePlanTotal(ExercisePlanBo bo) {
        return manageDao.getExercisePlanTotal(bo);
    }
    //生成明日锻炼计划
    @Override
    public ResultModel createExercisePlan(ExercisePlanPo po) {
        ResultModel resultModel = new ResultModel();

        Integer i = doPlan(po.getuId(),po.getEpRank());
        if(i==-999){
            //对不起，生成失败，请重试
            resultModel.setSuccess(false);
            resultModel.setMessage("对不起，生成失败，请重试");
            return resultModel;
        }else if(i==-555){
            //已存在
            resultModel.setSuccess(false);
            resultModel.setMessage("对不起,明日计划已存在，不能重复生成");
            return resultModel;
        }else if(i==0){
            //生成成功
            resultModel.setSuccess(true);
            resultModel.setMessage("生成成功");
            return resultModel;
        }else{
            int[] data = {999,777,555,333,111,888,666,444,222};
            doPlanDetail(data,i);
            //无历史锻炼记录，随机产生计划
            resultModel.setSuccess(true);
            resultModel.setMessage("生成成功");
            return resultModel;
        }
    }


    //锻炼计划详细信息查看
    @Override
    public List<PlanDetailVo> getPlanDetailList(PlanDetailBo bo) {
        return manageDao.getPlanDetailList(bo);
    }

    //获取个人锻炼记录
    @Override
    public List<DoPlanVo> getPersonalRecordList(Integer id) {
        return manageDao.getRecordById(id);
    }

    //获取个人明日计划
    @Override
    public List<PlanDetailVo> getPersonalPlanDetailList(ExercisePlanBo bo) {
        bo.setEpTime(DateGetUtil.getTomorrow());
        return manageDao.getPersonalPlanDetailList(bo);
    }


    //生成明日锻炼计划
    private Integer doPlan(Integer id,int i){

        ExercisePlanBo planBo2 = new ExercisePlanBo();
        planBo2.setuId(id);
        planBo2.setEpTime(DateGetUtil.getTomorrow());
        planBo2.setEpRank(i);
        planBo2.setEpIsValid("T");
        List<ExercisePlanVo> listPlanVo2 = manageDao.getExercisePlanList(planBo2);
        if(listPlanVo2!=null&&listPlanVo2.size()>0){
            return -555;
        }

        //写表
        ExercisePlanPo po1 = new ExercisePlanPo();
        //先写锻炼计划主表
        po1.setuId(id);
        po1.setEpTime(DateGetUtil.getTomorrow());
        po1.setEpRank(i);
        manageDao.createExercisePlan(po1);
        //查出主键
        ExercisePlanBo planBo = new ExercisePlanBo();
        planBo.setuId(id);
        planBo.setEpTime(DateGetUtil.getTomorrow());
        planBo.setEpRank(i);
        planBo.setEpIsValid("T");
        List<ExercisePlanVo> listPlanVo = manageDao.getExercisePlanList(planBo);
        if(listPlanVo==null||listPlanVo.size()<1){
            return -999;
        }

        //根据用户id查出历史锻炼记录
        List<DoPlanVo> list = manageDao.getRecordById(id);
        if(list==null||list.size()<1){
            return listPlanVo.get(0).getEpId();
        }

        //根据不同时间，增加附加值
        for(int j=0;j>-i;j--){
            for(DoPlanVo vo1 : list){
                if(vo1!=null&&vo1.getTodayTime().equals(DateGetUtil.getOtherDay(j))){
                    vo1.setDoTime(vo1.getDoTime()*(i+j));
                }
            }
        }

        //根据不同锻炼类型，累加时间
        //跑步机
        int time111 = 0;
        //划船器
        int time222 = 0;
        //肌肉组
        int time333 = 0;
        //仰卧器
        int time444 = 0;
        //健步车
        int time555 = 0;
        //美腰机
        int time666 = 0;
        //登梯器
        int time777 = 0;
        //引体杆
        int time888 = 0;
        //瑜伽球
        int time999 = 0;
        for(DoPlanVo vo2 : list){
            if(vo2.geteType()==111){
                time111 = time111 + vo2.getDoTime();
            }else if(vo2.geteType()==222){
                time222 = time222 + vo2.getDoTime();
            }else if(vo2.geteType()==333){
                time333 = time333 + vo2.getDoTime();
            }else if(vo2.geteType()==444){
                time444 = time444 + vo2.getDoTime();
            }else if(vo2.geteType()==555){
                time555 = time555 + vo2.getDoTime();
            }else if(vo2.geteType()==666){
                time666 = time666 + vo2.getDoTime();
            }else if(vo2.geteType()==777){
                time777 = time777 + vo2.getDoTime();
            }else if(vo2.geteType()==888){
                time888 = time888 + vo2.getDoTime();
            }else if(vo2.geteType()==999){
                time999 = time999 + vo2.getDoTime();
            }else{

            }
        }

        int[] dotime ={time111,time222,time333,time444,time555,time666,time777,time888,time999};
        int[] data ={111,222,333,444,555,666,777,888,999};
        //从大到小排序
        for(int k=0;k<dotime.length;k++){
            for(int j=k+1;j<dotime.length;j++){
                if(dotime[k]<dotime[j]){
                    int flag = dotime[k];
                    dotime[k] = dotime[j];
                    dotime[j] = flag;
                    int dataflag = data[k];
                    data[k] = data[j];
                    data[j] = dataflag;
                }
            }
        }
        //选择锻炼时间最少的四个给予锻炼计划
        doPlanDetail(data,listPlanVo.get(0).getEpId());
        return 0;
    }

    private String getGoal(int i){
        if(i==111){
            return "主要用以锻炼腿、臀、腰、腹部肌肉及心肺功能";
        }else if(i==222){
            return "主要用来增强手臂力量、背阔肌和动作协调能力";
        }else if(i==333){
            return "增加了锻炼的多样性和有效性。零阻力的锻炼减少肌肉劳损的发生";
        }else if(i==444){
            return "主要用来锻炼腰肌群，减少腰腹部多余脂肪";
        }else if(i==555){
            return "主要用以锻炼腿、腰、腹部肌肉及心肺功能";
        }else if(i==666){
            return "可对腰部、背部作放松按摩";
        }else if(i==777){
            return "锻炼腿部肌肉，增强体力";
        }else if(i==888){
            return "主要是用来锻炼上肢力量及胸大肌力量";
        }else if(i==999){
            return "主要锻炼身体柔韧性，美化形体";
        }else{
            return null;
        }
    }

    private void doPlanDetail(int[] data,int i){
        PlanDetailPo po01 = new PlanDetailPo();
        for(int q=8;q>4;q--){
            po01.setEpId(i);
            po01.setFtType(data[q]);
            po01.setdDuration(q*5);
            po01.setdGoal(getGoal(data[q]));
            manageDao.createPlanDetail(po01);
        }
    }

    @Override
    public List<DoPlanVo> getRecordByeId(Integer eid) {
        List<DoPlanVo> list1 = manageDao.getRecordByeId(eid);
        if(list1==null||list1.size()<1){
            return null;
        }
        List<DoPlanVo> list2 = new ArrayList<DoPlanVo>();
        for(DoPlanVo vo1:list1){
            if(list2.size()<1){
                list2.add(vo1);
            }else{
                boolean flag = true;
                for(DoPlanVo vo2:list2){
                    if(vo2!=null&&vo1.getTodayTime().equals(vo2.getTodayTime())){
                        vo2.setDoTime(vo2.getDoTime()+vo1.getDoTime());
                        flag=false;
                    }
                }
                if(flag){
                    list2.add(vo1);
                }
            }
        }
        return list2;
    }

    @Override
    public List<DoPlanVo> getAllRecord() {
        List<DoPlanVo> list1 = manageDao.getAllRecord();
        if(list1==null||list1.size()<1){
            return null;
        }
        List<DoPlanVo> list2 = new ArrayList<DoPlanVo>();
        for(DoPlanVo vo1:list1){
            if(list2.size()<1){
                list2.add(vo1);
            }else{
                boolean flag = true;
                for(DoPlanVo vo2:list2){
                    if(vo2!=null&&vo1.geteType().equals(vo2.geteType())){
                        vo2.setDoTime(vo2.getDoTime()+vo1.getDoTime());
                        flag=false;
                    }
                }
                if(flag){
                    list2.add(vo1);
                }
            }
        }
        return list2;
    }
}
